/*
 * StandardList.fx
 *
 * Created on 06.02.2010, 16:21:37
 */

package crudfx.control;
import javafx.scene.*;
import javafx.scene.paint.*;
import javafx.scene.image.*;
import crudfx.data.*;
import crudfx.interop.*;
import crudfx.control.*;
import crudfx.field.*;
import crudfx.util.*;
import javafx.stage.*;
import javafx.scene.shape.*;
import javafx.scene.input.*;
import javax.swing.table.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
/**
 * @author 1024
 * <pre>
var nn:Integer=2;
Stage {
    width:400
    height:300
    scene:Scene {
	content:[
            UINode{
                width:200
                height:100
                content:StandardList{
                    key:"value"
                    row:bind nn with inverse
                    data:[
                        Fields{fields:[NamedValue{name:"value" value:"first"}]}
                        Fields{fields:[NamedValue{name:"value" value:"second"}]}
                        Fields{fields:[NamedValue{name:"value" value:"third"}]}
                        Fields{fields:[NamedValue{name:"value" value:"fourth"}]}
                        Fields{fields:[NamedValue{name:"value" value:"fifth"}]}
                        Fields{fields:[NamedValue{name:"value" value:"sixth"}]}
                        Fields{fields:[NamedValue{name:"value" value:"seventh"}]}
                        ]
                    action:function():Void{
                        println(nn);
                        }
                    }
                }
            ]
        }
    }
</pre>
 */

public class StandardList extends UIComponent{
    var list:JList;
    var scrollPane:JScrollPane;
    var model:DefaultListModel;
    public var action:function():Void;
    public var data:Fields[];
    public var key:String;
    public var row:Integer=-1 on replace {
        select();
        };
    function select(){
        var n:Integer=0;
        if(model.getSize()>row and row>-1){
            list.setSelectedIndex(row);
            }
        else if(model.getSize()>0){
            row=0;
            list.setSelectedIndex(0);
            }
        }
    public function reset(){
        model.removeAllElements();
        for(f:Fields in data){
            model.addElement(f.get(key));
            }
        select();
        }
    init{
        scrollPane=new JScrollPane();
        list=new JList();
        model=new DefaultListModel();
        list.setModel(model);
        scrollPane.getViewport().add(list);
        scrollPane.getViewport().setOpaque(false);
        scrollPane.setOpaque(false);
        list.setOpaque(false);
        var sel:ListSelectionListener=ListSelectionListener{
            override function valueChanged(e:ListSelectionEvent):Void{
                if(model.getSize()>0){
                if(row!=list.getSelectedIndex()){
                    row=list.getSelectedIndex();
                    }
                }};
            };
        list.getSelectionModel().addListSelectionListener(sel);
        var mouseAdapter:MouseAdapter=MouseAdapter{
            override public function mousePressed(e:java.awt.event.MouseEvent):Void{
                //
                }
            override public function mouseReleased(e:java.awt.event.MouseEvent):Void{
                //
                }
            override public function mouseClicked(e:java.awt.event.MouseEvent):Void{
                if (e.getClickCount() == 2){
                    action();
                    }
                }
        }
        list.addMouseListener(mouseAdapter);
        reset();
        }
    override public function getJComponent(): JComponent{
        return scrollPane;
        }
    override public function highlight(s:String):Boolean{
        return false;
        }
    override public function gather():DataTree{
        var me:DataTree=DataTree{ name:"StandardList" };
        return me;
        }
    }
